//
//  YBGiftCell.m
//  yibeilive
//
//  Created by Itisnotthathard on 2022/11/18.
//

#import "YBGiftCell.h"

@implementation YBGiftCell

// 初始化控件
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        _giftImgV = [[UIImageView alloc] initWithFrame:CGRectZero];
        _giftImgV.contentMode = UIViewContentModeScaleAspectFit;
        [self.container addSubview:_giftImgV];
        
        _giftTitleLab = [[UILabel alloc] init];
        _giftTitleLab.font = kSystemFont(13);
        _giftTitleLab.textAlignment = NSTextAlignmentLeft;
        [self.container addSubview:_giftTitleLab];

        _giftLab = [[UILabel alloc] init];
        _giftLab.font = kSystemFont(13);
        _giftLab.textAlignment = NSTextAlignmentLeft;
        [self.container addSubview:_giftLab];
        
        _giftBtn = [[UIButton alloc] init];
        _giftBtn.titleLabel.font = kSystemFont(12.5);
        [_giftBtn addTarget:self action:@selector(giftBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [_giftBtn setTitle:@"回赠礼物" forState:UIControlStateNormal];
        [_giftBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
        [_giftBtn setTitle:@"继续送礼" forState:UIControlStateSelected];
        [_giftBtn setTitleColor:kMainColor forState:UIControlStateSelected];
        _giftBtn.clipsToBounds = YES;
        _giftBtn.layer.cornerRadius = 12.5;
        [self.container addSubview:_giftBtn];
    }
    return self;
}

// 根据 cellData 绘制 cell
- (void)fillWithData:(YBGiftCellData *)data {
    [super fillWithData:data];
    
    self.customData = data;
    
    if (data.direction==MsgDirectionIncoming) {
        self.bubbleView.image = [UIImage imageNamed:@"msg_gift_2"];
        
        NSString *str = data.getGiftUserName;
        NSString *str1 = [NSString stringWithFormat:@"送给%@",str];
        self.giftTitleLab.text = [NSString stringWithFormat:@"%@",(str && data.innerMessage.groupID.length > 0)?str1:@"收到礼物"];
        self.giftTitleLab.textAlignment = NSTextAlignmentLeft;
        self.giftTitleLab.textColor = kBlackColor;
        self.giftLab.textAlignment = NSTextAlignmentLeft;
        self.giftLab.textColor = kBlackColor;
        self.giftBtn.backgroundColor = kMainColor;
        self.giftBtn.selected = NO;
    }else{
        self.bubbleView.image = [UIImage imageNamed:@"msg_gift_1"];
        
        NSString *str = data.getGiftUserName;
        NSString *str1 = [NSString stringWithFormat:@"送给%@",str];
        self.giftTitleLab.text = [NSString stringWithFormat:@"%@",(str && data.innerMessage.groupID.length > 0)?str1:@"发出礼物"];
        self.giftTitleLab.textAlignment = NSTextAlignmentRight;
        self.giftTitleLab.textColor = UIColor.whiteColor;
        self.giftLab.textAlignment = NSTextAlignmentRight;
        self.giftLab.textColor = UIColor.whiteColor;
        self.giftBtn.backgroundColor = UIColor.whiteColor;
        self.giftBtn.selected = YES;
    }
    
    self.giftLab.text = [NSString stringWithFormat:@"%@ X%@",data.giftsName,data.giftsNum];
    [self.giftImgV sd_setImageWithURL:[NSURL URLWithString:data.img]];
}

// 设置控件坐标
- (void)layoutSubviews {
    [super layoutSubviews];
    
    if (self.customData.direction==MsgDirectionIncoming) {
        self.giftImgV.mm_width(self.container.mm_h - 10).mm_height(self.container.mm_h - 10).mm_top(0).mm_right(0);
        self.giftTitleLab.mm_sizeToFit().mm_top(15).mm_left(15).mm_flexToRight(self.container.mm_minX);
        self.giftLab.mm_sizeToFit().mm_top(self.giftTitleLab.mm_maxY + 10).mm_left(15).mm_flexToRight(self.container.mm_minX);
        self.giftBtn.mm_width(72).mm_height(25).mm_top(self.giftLab.mm_maxY + 10).mm_left(15);
    }else{
        self.giftImgV.mm_width(self.container.mm_h - 10).mm_height(self.container.mm_h - 10).mm_top(0).mm_left(0);
        self.giftTitleLab.mm_sizeToFit().mm_top(15).mm_left(self.giftImgV.mm_maxX).mm_flexToRight(15);
        self.giftLab.mm_sizeToFit().mm_top(self.giftTitleLab.mm_maxY + 10).mm_left(self.giftImgV.mm_maxX).mm_flexToRight(15);
        self.giftBtn.mm_width(72).mm_height(25).mm_top(self.giftLab.mm_maxY + 10).mm_right(15);
    }
}

#pragma mark - event

- (void)giftBtnClick:(UIButton *)btn {
    if(self.yBGiftCellDelegate && [self.yBGiftCellDelegate respondsToSelector:@selector(onClickGiftBtn:)]) {
        [self.yBGiftCellDelegate onClickGiftBtn:self];
    }
}

@end
